package com.qinglei.recoup.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qinglei.recoup.common.domain.*;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.common.utils.DateUtil;
import com.qinglei.recoup.system.domain.Bed;
import com.qinglei.recoup.system.manager.BreathHeartAlarmV4;
import com.qinglei.recoup.system.pojo.*;
import com.qinglei.recoup.system.service.*;
import com.qinglei.recoup.system.util.ProducerInstance;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "设备实时检测数据")
@RestController
@RequestMapping("/devicerealtimedata")
public class DeviceRealTimeDataController {
    @Autowired
    private DeviceRealTimeDataService deviceRealTimeDataService;
    @Resource
    private RadarUserDataService radarUserDataService;
    @Autowired
    private RadarUserDataFourService radarUserDataFourService;
    @Autowired
    private BedService bedService;
    @Autowired
    private BedInformationService bedInformationService;
    @Resource
    private EventLogService eventLogService;

    @ApiOperation(value = "获取床位列表接口-新")
    @GetMapping("/showList")
    @ApiImplicitParam(name = "sn", dataType = "String", required = true, value = "设备sn号", defaultValue = "")
    public CommonRes<List<BedStatusVO>> getBedShowList(String sn) {
        Long userId = 1L;
        Integer roleId = 2;
        List<BedStatusVO> list = deviceRealTimeDataService.getBedShowListByUser(sn, userId, roleId);
        return CommonRes.ok(list);
    }

    @ApiOperation(value = "护工和院长实时监控页面 获取床位实时监测数据-已完成-新")
    @ApiImplicitParam(name = "sn", dataType = "String", required = true, value = "设备sn号", defaultValue = "")
    @GetMapping("/realTimeData")
    public CommonRes getRealTimeData(@NotNull(message = "{required}") String sn, Integer second) throws RedisConnectException {
        Bed bed = deviceRealTimeDataService.getBedBySn(sn);
        if (bed == null) {
//            return CommonRes.failed("床位不存在");
            return CommonRes.ok();
        }

        if (StringUtils.isEmpty(bed.getRadarVersion())) {
            return CommonRes.failed("暂时收不到数据，请尝试重新连接电源并且配网");
        }
        Integer tenantId = 1;
        if (bed.getRadarVersion().equals("3_0")) {
            RealTimeDataVO realTimeDataVO = radarUserDataService.getRealTimeData(bed.getId());
            if (realTimeDataVO == null) {
                return CommonRes.failed("床位或者设备信息不存在");
            }
            return CommonRes.ok(realTimeDataVO);
        } else if (bed.getRadarVersion().equals("4_0")) {
            RealTimeDataVOV4 realTimeDataVOV4 = radarUserDataFourService.getRealTimeData(bed.getId(), second, tenantId);
            if (realTimeDataVOV4 == null) {
                return CommonRes.failed("床位或者设备信息不存在");
            }
            return CommonRes.ok(realTimeDataVOV4);
        }

        return null;
    }

    @ApiOperation(value = "护工端和院长端 实时监控页面 获取监控基本信息（返回开关状态和监测时间段信息）-新")
    @ApiImplicitParam(name = "sn", dataType = "String", required = true, value = "设备sn号", defaultValue = "")
    @GetMapping("/getMonitorInfo")
    public CommonRes<MonitorInfoVO> getMonitorInfo(@NotNull(message = "{required}") String sn) {
        Bed bed = deviceRealTimeDataService.getBedBySn(sn);
        if (null != bed) {
            log.info("getMonitorInfo param:{}", bed.getId());
            CommonRes<MonitorInfoVO> monitorInfo = bedService.getMonitorInfo(bed.getId());
            log.info("getMonitorInfo result:{}", monitorInfo);
            return monitorInfo;
        } else {
            return CommonRes.ok();
        }

    }

    @ApiOperation(value = "获取用户情况备注-新")
    @ApiImplicitParam(name = "sn", dataType = "String", required = true, value = "设备sn号", defaultValue = "")
    @GetMapping("/getUserInfoBySn")
    public CommonRes getByBedId(String sn) {
        Bed bed = deviceRealTimeDataService.getBedBySn(sn);
        if (null != bed) {
            BedInformationVO bedInformation = deviceRealTimeDataService.getBedInformationBySn(sn);
            return CommonRes.ok(bedInformation);
        }
//        else {
//            BedInformationVO bedInformation = deviceRealTimeDataService.getBedInformationBySn(sn);
//            return CommonRes.ok(bedInformation);
//        }
        return CommonRes.ok();
    }

    @ApiOperation(value = "获取床位历史信息-新")
    @ApiImplicitParam(name = "sn", dataType = "String", required = true, value = "设备sn号", defaultValue = "")
    @GetMapping("/bed")
    public CommonRes<List<EventLogVO>> getBedEventList(@Valid DeviceRealBedParam param) {
        Bed bed = deviceRealTimeDataService.getBedBySn(param.getSn());
        if (null != bed) {
            log.info("getBedEvent in param:{}", param);
            BedEventParam bedEventParam = new BedEventParam();
            bedEventParam.setLastId(0L);
            if (null == param.getSize()) {
                bedEventParam.setSize(10);
            }
            bedEventParam.setBedId(bed.getId());

            List<EventLogVO> bedEventList = eventLogService.getBedEventList(bedEventParam);
            return CommonRes.ok(bedEventList);
        } else {
            return CommonRes.ok();
        }
    }

    @Value("${device-event-topic}")
    private String deviceTopic;
    @Value("${device-event-group}")
    private String deviceGroup;

    @ApiOperation(value = "测试-手动插入实时检测数据调用接口")
    @GetMapping("/insertRadarUserDataFour")
    public CommonRes<List<EventLogVO>> insertRadarUserDataFour(RadarUserDataFour param) {
        if (StringUtils.isNotBlank(param.getDataTime())) {
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FULL_TIME_SPLIT_PATTERN);
            try {
                param.setCreateTime(sdf.parse(param.getDataTime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        radarUserDataFourService.save(param);
        return CommonRes.ok();
    }

    @Resource
    private RadarService radarService;
    @Value("${Health.onOrOffV3}")
    private boolean onOrOffV3;
    @Resource
    private BreathHeartAlarmV4 breathHeartAlarmV4;

    @ApiOperation(value = "实时数据-手动模拟PUSH")
    @PostMapping("/pushRealData")
    public CommonRes pushRealData(@RequestBody RadarMessage message) {
//        RadarMessage message = JSON.parseObject(messagestr, RadarMessage.class);
        RadarMessage msg = new RadarMessage();
        List<RadarUserDataFour> datas = JSON.parseArray("[{\"aAFg\":0,\"bdef\":1.150898,\"brebp\":10,\"bs\":\"[0.031488, -0.12028, -0.259794, -0.252472, -0.126008, -0.04655, -0.076714, -0.101361, -0.04435, 0.032985]\",\"createTime\":1656675754350,\"frameNo\":498805,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.018654, -0.028126, -0.024552, -0.006216, 0.016167, 0.030018, 0.035708, 0.034451, 0.015019, -0.017857]\",\"id\":\"120a501d45254409b0708b3773508a69\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":6.988157,\"brebp\":10,\"bs\":\"[-0.316091, -0.979918, 2.751047, 9.229886, 14.341656, 13.587366, 6.950535, -1.700491, -9.003284, -12.822856]\",\"createTime\":1656675761893,\"frameNo\":498814,\"hPhERB\":20,\"hRBpRe\":56,\"hs\":\"[0.647926, 0.082747, -0.312195, -0.367651, -0.28277, -0.15997, 0.007533, 0.077253, -0.055754, -0.193074]\",\"id\":\"1693fe74146f4e5a89c71bc92f7cd549\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.150898,\"brebp\":10,\"bs\":\"[0.052313, -0.01381, -0.106583, -0.120134, -0.041114, 0.037598, 0.01798, -0.085342, -0.138777, -0.079748]\",\"createTime\":1656675754825,\"frameNo\":498806,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.030418, -0.011378, 0.000425, -0.020529, -0.035901, -0.005767, 0.036658, 0.033186, -0.00775, -0.028011]\",\"id\":\"252ca67f1c894654b28b09c00d45520e\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.170321,\"brebp\":10,\"bs\":\"[-0.106627, -0.033712, 0.151997, 0.243283, 0.173054, 0.023162, -0.095339, -0.046653, 0.145482, 0.243601]\",\"createTime\":1656675757073,\"frameNo\":498809,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.004885, 0.036061, 0.046305, 0.0069, -0.03126, -0.016559, 0.02211, 0.025979, -0.000325, -0.01363]\",\"id\":\"3cfaa652d0f845668f875be32ad77511\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.086251, 0.098779, 0.136478, 0.15306, 0.121312, 0.073953, 0.049667, 0.048232, 0.052245, 0.056178]\",\"createTime\":1656675742890,\"frameNo\":498795,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-2.055678, -0.971064, -0.452883, -0.025156, 1.327151, 3.665946, 5.070055, 3.605522, 0.026719, -3.372227]\",\"id\":\"416e34584ca941a3829fc3b8407a2fb2\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.376915,\"brebp\":10,\"bs\":\"[0.158954, 0.185227, 0.199326, 0.205618, 0.206424, 0.200063, 0.207964, 0.24293, 0.261267, 0.213363]\",\"createTime\":1656675737888,\"frameNo\":498790,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.610377, 2.712853, 3.207902, 1.101948, -0.810008, -1.134296, -0.77748, -0.813536, -0.883387, -0.082562]\",\"id\":\"55978fbff8ff4be3af022827526ec610\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":6.049641,\"brebp\":10,\"bs\":\"[2.126572, 5.068101, 8.99702, 11.380957, 11.9597, 12.612711, 13.934287, 13.993373, 10.568949, 4.512883]\",\"createTime\":1656675760867,\"frameNo\":498813,\"hPhERB\":20,\"hRBpRe\":56,\"hs\":\"[0.417921, 0.538474, 0.320221, -0.261421, -0.72834, -0.704466, -0.391058, 0.010541, 0.51687, 0.853939]\",\"id\":\"57903a51351847238b7852292369392b\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.203919, -0.03036, 0.073779, 0.071849, 0.037225, 0.037076, 0.049962, 0.026869, 0.000078, 0.020789]\",\"createTime\":1656675748854,\"frameNo\":498801,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.122149, -0.095208, -0.087756, -0.076673, -0.024572, 0.064219, 0.121407, 0.068112, -0.0371, -0.027409]\",\"id\":\"57d67b5217b34b3f828af94d771cd31f\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.170321,\"brebp\":10,\"bs\":\"[-0.041278, 0.021339, 0.009308, -0.098788, -0.181959, -0.095714, 0.133904, 0.287535, 0.216902, 0.017593]\",\"createTime\":1656675757018,\"frameNo\":498808,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.008909, 0.000871, 0.019315, 0.037461, 0.027622, -0.001184, -0.022843, -0.029862, -0.033304, -0.03097]\",\"id\":\"5fd877e9b21e410d99912fb8fc3b512e\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.201237,\"brebp\":10,\"bs\":\"[-0.395828, -2.242055, -4.360136, -5.425426, -5.40727, -4.928838, -4.39506, -3.995107, -3.663865, -3.29472]\",\"createTime\":1656675757885,\"frameNo\":498810,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.007384, -0.000444, 0.002968, 0.00218, -0.007086, -0.020025, -0.026858, -0.018332, 0.006052, 0.027275]\",\"id\":\"658ff88c28204fef9117221e021bcf38\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.170321,\"brebp\":10,\"bs\":\"[0.027952, 0.141135, 0.239311, 0.256273, 0.152776, -0.004526, -0.093682, -0.084997, -0.069931, -0.077225]\",\"createTime\":1656675756870,\"frameNo\":498807,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.008055, 0.01958, 0.03343, 0.034991, 0.014324, -0.03032, -0.061081, -0.042116, 0.003685, 0.023262]\",\"id\":\"6a2deab4c5784e1589c914928e5bc892\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.076444, 0.086678, 0.108128, 0.126629, 0.130615, 0.121877, 0.114522, 0.115597, 0.114659, 0.100754]\",\"createTime\":1656675741876,\"frameNo\":498794,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[3.944112, 3.009043, 0.17851, -1.814501, -0.867896, 1.6308, 2.328562, 0.25069, -2.27642, -2.980633]\",\"id\":\"72d5984824a8425baf3715f0b6f97a38\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.030311, 0.007404, 0.009672, -0.019469, -0.045333, -0.053715, -0.035638, -0.045634, -0.162297, -0.271685]\",\"createTime\":1656675747888,\"frameNo\":498800,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.151546, 0.058437, 0.227563, 0.306953, 0.296376, 0.225558, 0.120802, 0.001481, -0.098212, -0.138681]\",\"id\":\"7b97911fbedd408faf04b9528bddcc09\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.744425,\"brebp\":10,\"bs\":\"[-2.89149, -2.515527, -2.184506, -1.865735, -1.559247, -1.299303, -1.090772, -0.900678, -0.69978, -0.480525]\",\"createTime\":1656675758864,\"frameNo\":498811,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.068315, 0.182195, 0.197283, -0.15919, -0.628467, -0.560619, 0.127401, 0.737552, 0.664362, 0.111604]\",\"id\":\"8444bcb907914908bd0f44f0800d70b3\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.156225, 0.152208, 0.138413, 0.127643, 0.13196, 0.136735, 0.125054, 0.106499, 0.092672, 0.081336]\",\"createTime\":1656675740868,\"frameNo\":498793,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.443319, -0.552447, -0.976491, -0.225478, -0.296537, -1.936094, -3.494931, -3.08454, -0.65857, 2.30591]\",\"id\":\"8a14cf6d20d94cb29128c08f9242bd50\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":3.966213,\"brebp\":10,\"bs\":\"[-0.251385, -0.034545, 0.163577, 0.352266, 0.520828, 0.659272, 0.782564, 0.914125, 1.059316, 1.195204]\",\"createTime\":1656675759800,\"frameNo\":498812,\"hPhERB\":20,\"hRBpRe\":56,\"hs\":\"[-0.295144, -0.309736, -0.116777, 0.066986, 0.091716, -0.083839, -0.271889, -0.257158, -0.059947, 0.187037]\",\"id\":\"8dda74b3c991447f867fa766c9194625\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.207478,\"brebp\":10,\"bs\":\"[0.064428, 0.083536, 0.119738, 0.151354, 0.135902, 0.077015, 0.025814, 0.016433, 0.035542, 0.051048]\",\"createTime\":1656675743828,\"frameNo\":498796,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-5.226653, -4.97817, -2.932329, -1.026136, -0.641032, -0.018642, 3.560601, 8.818769, 9.951418, 3.719813]\",\"id\":\"9b25bb0524fe4f0e9dd275958d910a7e\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.097403, -0.060158, 0.075224, 0.109874, -0.015123, -0.201954, -0.285793, -0.133039, 0.117651, 0.177892]\",\"createTime\":1656675750923,\"frameNo\":498803,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.057863, 0.077411, 0.053547, 0.022736, 0.005788, -0.011131, -0.033041, -0.04183, -0.036198, -0.031925]\",\"id\":\"b6ad5341e46444b2b4d87302e567191b\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.436837, -0.227177, 0.036447, 0.098907, 0.043518, 0.007817, 0.007952, -0.005791, -0.044967, -0.060914]\",\"createTime\":1656675746907,\"frameNo\":498799,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.113713, 1.197739, 0.944687, 0.455286, -0.051173, -0.386328, -0.517902, -0.521399, -0.458269, -0.335663]\",\"id\":\"d02431d979e44689a985420a70102323\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[0.05132, 0.041556, 0.016763, 0.012931, 0.018043, 0.012658, 0.002944, -0.002318, 0.001438, -0.030329]\",\"createTime\":1656675749920,\"frameNo\":498802,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.091095, 0.127059, 0.018987, -0.0919, -0.09495, -0.037267, -0.015455, -0.039781, -0.046027, -0.000737]\",\"id\":\"d09e332d1cab482eaf4f38ac7c606014\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.207478,\"brebp\":10,\"bs\":\"[0.049729, 0.037576, 0.021879, 0.008067, 0.001232, 0.003676, 0.008227, 0.003063, -0.032209, -0.10279]\",\"createTime\":1656675744861,\"frameNo\":498797,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-4.892299, -8.455787, -6.188095, -2.971106, -1.734444, -0.992258, 0.861654, 2.788021, 3.454103, 3.111888]\",\"id\":\"d5316ff6ddbb4991a3af800439db6032\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.248868, 0.249011, 0.24138, 0.248769, 0.264495, 0.256617, 0.223131, 0.187146, 0.16403, 0.156101]\",\"createTime\":1656675739827,\"frameNo\":498792,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.125974, 0.458326, -1.628616, -2.190854, -1.018836, -0.299133, -0.301575, 0.737041, 2.654685, 3.136738]\",\"id\":\"e35265bb0ce44cc18295115513cac6f0\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.124014, 0.07053, 0.083983, 0.125116, 0.143342, 0.133498, 0.147176, 0.201678, 0.240676, 0.245531]\",\"createTime\":1656675738775,\"frameNo\":498791,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.038973, 0.75676, -1.066301, -2.127104, -0.584068, 2.124299, 2.691497, 0.44743, -1.398249, -0.48339]\",\"id\":\"e5cc30f11a5a41c0b25545fc96802fb2\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.161991,\"brebp\":10,\"bs\":\"[0.011959, -0.099742, 0.033644, 0.200653, 0.10058, -0.192735, -0.351182, -0.244131, -0.040336, 0.070156]\",\"createTime\":1656675754329,\"frameNo\":498804,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.022924, 0.008546, 0.048707, 0.059752, 0.032949, -0.000825, -0.016404, -0.014896, -0.008071, -0.008289]\",\"id\":\"f48dd26dd022443cb3147f8c461d7959\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.165203,\"brebp\":10,\"bs\":\"[-0.15827, -0.149179, -0.10686, -0.072442, -0.0153, 0.077223, 0.124339, 0.077661, -0.080285, -0.3318]\",\"createTime\":1656675745834,\"frameNo\":498798,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[2.275231, 0.945839, -0.553565, -1.572101, -1.920308, -1.831944, -1.434367, -0.754655, 0.038021, 0.707107]\",\"id\":\"fb3abd5705d742c89d5f71cb518ffc02\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0}]", RadarUserDataFour.class);
        msg.setData(datas.get(0));
        Date date = new Date();
        date.setTime(message.getTimestamp());
        log.info("------jssbscsj----" + JSON.toJSONString(message));
        // log.info("receive RadarMessage process:{}", JsonUtils.objectToJson(message));
        try {
            String proCode = message.getProCode();
            if (proCode == null) {
                //不解析没有协议号的数据
                return CommonRes.ok();
            }
            Bed bed = bedService.getBedBySn(message.getSn());
            if (bed != null) {
                if (!proCode.equals(bed.getRadarVersion())) {
                    UpdateWrapper<Bed> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id", bed.getId());
                    Bed bedUpdate = new Bed();
                    bedUpdate.setRadarVersion(proCode);
                    bedService.getBaseMapper().update(bedUpdate, updateWrapper);
                }
            } else {
                //不解析未绑定床位的数据
                return CommonRes.ok();
            }
            if (RadarVersionEnum.VERSION_V3.getCode().equals(proCode)) {
                if (message.getType() == RadarMessage.TYPE_USER_DATA) {
                    RadarUserDataDTO data = (RadarUserDataDTO) message.getData();
                    // 去除翻身状态
                    if (data.getS2() > 3) {
                        return CommonRes.ok();
                    }
                    // 保存雷达上传的用户数据
                    radarUserDataService.saveRadarUserData(message);
                    // 更新雷达在线状态
                    radarService.updateOnlineStatus(message.getSn());
                    //更新呼吸过速、呼吸暂停状态
                    if (onOrOffV3) {
//                        breathHeartAlarmV3.updateBreathStatus(message);
                    }

                } else if (message.getType() == RadarMessage.TYPE_FALL_DOWN_DATA) {
                    // 更新雷达在线状态
                    radarService.updateOnlineStatusDownData(message.getSn());
                }
                // 处理雷达关联的床位状态   包含跌倒相关数据
                bedService.procRadarData(message, false);
            } else if (RadarVersionEnum.VERSION_V4.getCode().equals(proCode)) {
                radarUserDataFourService.saveRadarUserData(message);
                radarService.updateOnlineStatus(message.getSn());
                radarService.updateNetworkStatus(message.getSn());
                bedService.procRadarDataV4(message);
                breathHeartAlarmV4.updateBreathStatus(message);
            }
        } catch (Exception e) {
            log.error("process RadarMessage error RadarMessage:{}", message, e);
        }
        return CommonRes.ok();
    }

    @Value("${alarm-event-topic}")
    private String alarmTopic;
    @Value("${alarm-event-group}")
    private String alarmGroup;
    @Value("${rocketmq.name-server}")
    private String rocketServer;

    @ApiOperation(value = "推送rocketmq消息 消息参数自定义")
    @PostMapping("/pushMqMsg")
    public CommonRes pushRealData(@RequestBody String message) {
        try {
            //推送跌倒报警
            MQProducer producer = ProducerInstance.getProducerInstance().getInstance(rocketServer, alarmGroup);
            Message msg = new Message(alarmTopic, message.getBytes(StandardCharsets.UTF_8));
            producer.sendOneway(msg);
        } catch (Exception e) {
            log.error("推送异常 ==>{}", e.getMessage());
        }
        return CommonRes.ok();
    }

    public static void main(String[] args) {
        RadarMessage msg = new RadarMessage();
        msg.setSn("A4E57CDAEA98");
        msg.setProCode("4.0");
        List<RadarUserDataFour> datas = JSON.parseArray("[{\"aAFg\":0,\"bdef\":1.150898,\"brebp\":10,\"bs\":\"[0.031488, -0.12028, -0.259794, -0.252472, -0.126008, -0.04655, -0.076714, -0.101361, -0.04435, 0.032985]\",\"createTime\":1656675754350,\"frameNo\":498805,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.018654, -0.028126, -0.024552, -0.006216, 0.016167, 0.030018, 0.035708, 0.034451, 0.015019, -0.017857]\",\"id\":\"120a501d45254409b0708b3773508a69\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":6.988157,\"brebp\":10,\"bs\":\"[-0.316091, -0.979918, 2.751047, 9.229886, 14.341656, 13.587366, 6.950535, -1.700491, -9.003284, -12.822856]\",\"createTime\":1656675761893,\"frameNo\":498814,\"hPhERB\":20,\"hRBpRe\":56,\"hs\":\"[0.647926, 0.082747, -0.312195, -0.367651, -0.28277, -0.15997, 0.007533, 0.077253, -0.055754, -0.193074]\",\"id\":\"1693fe74146f4e5a89c71bc92f7cd549\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.150898,\"brebp\":10,\"bs\":\"[0.052313, -0.01381, -0.106583, -0.120134, -0.041114, 0.037598, 0.01798, -0.085342, -0.138777, -0.079748]\",\"createTime\":1656675754825,\"frameNo\":498806,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.030418, -0.011378, 0.000425, -0.020529, -0.035901, -0.005767, 0.036658, 0.033186, -0.00775, -0.028011]\",\"id\":\"252ca67f1c894654b28b09c00d45520e\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.170321,\"brebp\":10,\"bs\":\"[-0.106627, -0.033712, 0.151997, 0.243283, 0.173054, 0.023162, -0.095339, -0.046653, 0.145482, 0.243601]\",\"createTime\":1656675757073,\"frameNo\":498809,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.004885, 0.036061, 0.046305, 0.0069, -0.03126, -0.016559, 0.02211, 0.025979, -0.000325, -0.01363]\",\"id\":\"3cfaa652d0f845668f875be32ad77511\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.086251, 0.098779, 0.136478, 0.15306, 0.121312, 0.073953, 0.049667, 0.048232, 0.052245, 0.056178]\",\"createTime\":1656675742890,\"frameNo\":498795,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-2.055678, -0.971064, -0.452883, -0.025156, 1.327151, 3.665946, 5.070055, 3.605522, 0.026719, -3.372227]\",\"id\":\"416e34584ca941a3829fc3b8407a2fb2\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.376915,\"brebp\":10,\"bs\":\"[0.158954, 0.185227, 0.199326, 0.205618, 0.206424, 0.200063, 0.207964, 0.24293, 0.261267, 0.213363]\",\"createTime\":1656675737888,\"frameNo\":498790,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.610377, 2.712853, 3.207902, 1.101948, -0.810008, -1.134296, -0.77748, -0.813536, -0.883387, -0.082562]\",\"id\":\"55978fbff8ff4be3af022827526ec610\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":6.049641,\"brebp\":10,\"bs\":\"[2.126572, 5.068101, 8.99702, 11.380957, 11.9597, 12.612711, 13.934287, 13.993373, 10.568949, 4.512883]\",\"createTime\":1656675760867,\"frameNo\":498813,\"hPhERB\":20,\"hRBpRe\":56,\"hs\":\"[0.417921, 0.538474, 0.320221, -0.261421, -0.72834, -0.704466, -0.391058, 0.010541, 0.51687, 0.853939]\",\"id\":\"57903a51351847238b7852292369392b\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.203919, -0.03036, 0.073779, 0.071849, 0.037225, 0.037076, 0.049962, 0.026869, 0.000078, 0.020789]\",\"createTime\":1656675748854,\"frameNo\":498801,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.122149, -0.095208, -0.087756, -0.076673, -0.024572, 0.064219, 0.121407, 0.068112, -0.0371, -0.027409]\",\"id\":\"57d67b5217b34b3f828af94d771cd31f\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.170321,\"brebp\":10,\"bs\":\"[-0.041278, 0.021339, 0.009308, -0.098788, -0.181959, -0.095714, 0.133904, 0.287535, 0.216902, 0.017593]\",\"createTime\":1656675757018,\"frameNo\":498808,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.008909, 0.000871, 0.019315, 0.037461, 0.027622, -0.001184, -0.022843, -0.029862, -0.033304, -0.03097]\",\"id\":\"5fd877e9b21e410d99912fb8fc3b512e\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.201237,\"brebp\":10,\"bs\":\"[-0.395828, -2.242055, -4.360136, -5.425426, -5.40727, -4.928838, -4.39506, -3.995107, -3.663865, -3.29472]\",\"createTime\":1656675757885,\"frameNo\":498810,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.007384, -0.000444, 0.002968, 0.00218, -0.007086, -0.020025, -0.026858, -0.018332, 0.006052, 0.027275]\",\"id\":\"658ff88c28204fef9117221e021bcf38\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.170321,\"brebp\":10,\"bs\":\"[0.027952, 0.141135, 0.239311, 0.256273, 0.152776, -0.004526, -0.093682, -0.084997, -0.069931, -0.077225]\",\"createTime\":1656675756870,\"frameNo\":498807,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.008055, 0.01958, 0.03343, 0.034991, 0.014324, -0.03032, -0.061081, -0.042116, 0.003685, 0.023262]\",\"id\":\"6a2deab4c5784e1589c914928e5bc892\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.076444, 0.086678, 0.108128, 0.126629, 0.130615, 0.121877, 0.114522, 0.115597, 0.114659, 0.100754]\",\"createTime\":1656675741876,\"frameNo\":498794,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[3.944112, 3.009043, 0.17851, -1.814501, -0.867896, 1.6308, 2.328562, 0.25069, -2.27642, -2.980633]\",\"id\":\"72d5984824a8425baf3715f0b6f97a38\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.030311, 0.007404, 0.009672, -0.019469, -0.045333, -0.053715, -0.035638, -0.045634, -0.162297, -0.271685]\",\"createTime\":1656675747888,\"frameNo\":498800,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.151546, 0.058437, 0.227563, 0.306953, 0.296376, 0.225558, 0.120802, 0.001481, -0.098212, -0.138681]\",\"id\":\"7b97911fbedd408faf04b9528bddcc09\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.744425,\"brebp\":10,\"bs\":\"[-2.89149, -2.515527, -2.184506, -1.865735, -1.559247, -1.299303, -1.090772, -0.900678, -0.69978, -0.480525]\",\"createTime\":1656675758864,\"frameNo\":498811,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.068315, 0.182195, 0.197283, -0.15919, -0.628467, -0.560619, 0.127401, 0.737552, 0.664362, 0.111604]\",\"id\":\"8444bcb907914908bd0f44f0800d70b3\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.156225, 0.152208, 0.138413, 0.127643, 0.13196, 0.136735, 0.125054, 0.106499, 0.092672, 0.081336]\",\"createTime\":1656675740868,\"frameNo\":498793,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.443319, -0.552447, -0.976491, -0.225478, -0.296537, -1.936094, -3.494931, -3.08454, -0.65857, 2.30591]\",\"id\":\"8a14cf6d20d94cb29128c08f9242bd50\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":3.966213,\"brebp\":10,\"bs\":\"[-0.251385, -0.034545, 0.163577, 0.352266, 0.520828, 0.659272, 0.782564, 0.914125, 1.059316, 1.195204]\",\"createTime\":1656675759800,\"frameNo\":498812,\"hPhERB\":20,\"hRBpRe\":56,\"hs\":\"[-0.295144, -0.309736, -0.116777, 0.066986, 0.091716, -0.083839, -0.271889, -0.257158, -0.059947, 0.187037]\",\"id\":\"8dda74b3c991447f867fa766c9194625\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.207478,\"brebp\":10,\"bs\":\"[0.064428, 0.083536, 0.119738, 0.151354, 0.135902, 0.077015, 0.025814, 0.016433, 0.035542, 0.051048]\",\"createTime\":1656675743828,\"frameNo\":498796,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-5.226653, -4.97817, -2.932329, -1.026136, -0.641032, -0.018642, 3.560601, 8.818769, 9.951418, 3.719813]\",\"id\":\"9b25bb0524fe4f0e9dd275958d910a7e\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.097403, -0.060158, 0.075224, 0.109874, -0.015123, -0.201954, -0.285793, -0.133039, 0.117651, 0.177892]\",\"createTime\":1656675750923,\"frameNo\":498803,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.057863, 0.077411, 0.053547, 0.022736, 0.005788, -0.011131, -0.033041, -0.04183, -0.036198, -0.031925]\",\"id\":\"b6ad5341e46444b2b4d87302e567191b\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[-0.436837, -0.227177, 0.036447, 0.098907, 0.043518, 0.007817, 0.007952, -0.005791, -0.044967, -0.060914]\",\"createTime\":1656675746907,\"frameNo\":498799,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.113713, 1.197739, 0.944687, 0.455286, -0.051173, -0.386328, -0.517902, -0.521399, -0.458269, -0.335663]\",\"id\":\"d02431d979e44689a985420a70102323\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.151169,\"brebp\":10,\"bs\":\"[0.05132, 0.041556, 0.016763, 0.012931, 0.018043, 0.012658, 0.002944, -0.002318, 0.001438, -0.030329]\",\"createTime\":1656675749920,\"frameNo\":498802,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[0.091095, 0.127059, 0.018987, -0.0919, -0.09495, -0.037267, -0.015455, -0.039781, -0.046027, -0.000737]\",\"id\":\"d09e332d1cab482eaf4f38ac7c606014\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.207478,\"brebp\":10,\"bs\":\"[0.049729, 0.037576, 0.021879, 0.008067, 0.001232, 0.003676, 0.008227, 0.003063, -0.032209, -0.10279]\",\"createTime\":1656675744861,\"frameNo\":498797,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-4.892299, -8.455787, -6.188095, -2.971106, -1.734444, -0.992258, 0.861654, 2.788021, 3.454103, 3.111888]\",\"id\":\"d5316ff6ddbb4991a3af800439db6032\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.248868, 0.249011, 0.24138, 0.248769, 0.264495, 0.256617, 0.223131, 0.187146, 0.16403, 0.156101]\",\"createTime\":1656675739827,\"frameNo\":498792,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.125974, 0.458326, -1.628616, -2.190854, -1.018836, -0.299133, -0.301575, 0.737041, 2.654685, 3.136738]\",\"id\":\"e35265bb0ce44cc18295115513cac6f0\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.345943,\"brebp\":10,\"bs\":\"[0.124014, 0.07053, 0.083983, 0.125116, 0.143342, 0.133498, 0.147176, 0.201678, 0.240676, 0.245531]\",\"createTime\":1656675738775,\"frameNo\":498791,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[1.038973, 0.75676, -1.066301, -2.127104, -0.584068, 2.124299, 2.691497, 0.44743, -1.398249, -0.48339]\",\"id\":\"e5cc30f11a5a41c0b25545fc96802fb2\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.161991,\"brebp\":10,\"bs\":\"[0.011959, -0.099742, 0.033644, 0.200653, 0.10058, -0.192735, -0.351182, -0.244131, -0.040336, 0.070156]\",\"createTime\":1656675754329,\"frameNo\":498804,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[-0.022924, 0.008546, 0.048707, 0.059752, 0.032949, -0.000825, -0.016404, -0.014896, -0.008071, -0.008289]\",\"id\":\"f48dd26dd022443cb3147f8c461d7959\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0},{\"aAFg\":0,\"bdef\":1.165203,\"brebp\":10,\"bs\":\"[-0.15827, -0.149179, -0.10686, -0.072442, -0.0153, 0.077223, 0.124339, 0.077661, -0.080285, -0.3318]\",\"createTime\":1656675745834,\"frameNo\":498798,\"hPhERB\":29,\"hRBpRe\":56,\"hs\":\"[2.275231, 0.945839, -0.553565, -1.572101, -1.920308, -1.831944, -1.434367, -0.754655, 0.038021, 0.707107]\",\"id\":\"fb3abd5705d742c89d5f71cb518ffc02\",\"oBFg\":0,\"sFg\":0,\"sn\":\"A4E57CDAEA98\",\"tCFg\":0,\"tSFg\":0}]", RadarUserDataFour.class);
        msg.setData(datas.get(0));
        System.out.println(JSON.toJSONString(msg));
    }
}
